package fr.utbm.aiechat.activities;

import fr.utbm.aiechat.Network;
import fr.utbm.aiechat.structures.Channel;
import fr.utbm.aiechat.structures.User;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class UsersTabActivity extends ListActivity
{
	ListView lv;
	User userSelected = null;
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setListAdapter(Network.getInstance().Users);
		
		lv = getListView();
		lv.setTextFilterEnabled(true);
		
		lv.setOnItemClickListener(new OnItemClickListener()
			{
				public void onItemClick(AdapterView<?> parent, View view,int position, long id)
				{
					userSelected = (User) lv.getItemAtPosition(position);
					showDialog(position);
				}
		});
	}
	
	protected Dialog onCreateDialog(final int id)
	{
	    Dialog dialog;
	   /* switch(id)
	    {
	    case :*/
	    	
	    	final CharSequence[] items = {"Lancer Chat", "Voir Position", "Voir Fichiers"};
	    	//Important dans le builder mettre Activity.this pour avoir les attributs de l'objet
			AlertDialog.Builder builder = new AlertDialog.Builder(UsersTabActivity.this);
			builder.setTitle("Actions");
			builder.setItems(items, new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog, int item) {
			    	switch (item) {
			    		case 0 : 
			    			//Run Chat
			    			Bundle b1 = new Bundle();
			    			System.out.println("address = " + userSelected.getAddress());
			    			Network network = Network.getInstance();
			    			Channel chan = network.createChannel("Conversation avec : " + Network.getInstance().me.getName() + ", " + userSelected.getName(), true);	
			    			chan.addUser(userSelected);
			    			System.out.println("UserList = " + chan.getListUser().toString());
			    			network.inviteUserToChan(userSelected.getAddress(), chan,true);
			    			b1.putInt("chanSelected", chan.getUniqueID());
			            	Intent MessActivity = new Intent(getBaseContext(), MessActivity.class);
			            	MessActivity.putExtras(b1);
			            	startActivity(MessActivity);
			    		break;
			    		case 1 :
			    			Intent MapViewActivity = new Intent(getBaseContext(), MapViewActivity.class);
				    		Bundle b = new Bundle();
				    		b.putString("typeView", "oneUser");
				    		b.putInt("idUser", id);
				    		MapViewActivity.putExtras(b);
			            	startActivity(MapViewActivity);	
			    		break;
			    		/*case 2 :
			    			Intent UserInfoActivity = new Intent(getBaseContext(), UserInfoActivity.class);
			    			Bundle b2 = new Bundle();
				    		b2.putInt("idUser", id);
				    		UserInfoActivity.putExtras(b2);
				    		startActivity(UserInfoActivity);
			    		break;*/
			    		case 2:
			    			Network.getInstance().askForFilesList(userSelected);
			    		break;
			    	}
			    }
			});
			dialog = builder.create();
			
	    /*    break;
	    default:
	        dialog = null;
	    }*/
	    return dialog;
	}
}